package org.academic.texteditor.gui.feature;

import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import org.academic.texteditor.gui.helper.PageSize;
import org.academic.texteditor.gui.tool.Decoration;
import org.academic.texteditor.tool.Margin;

public class Page extends Feature {

	private static final long serialVersionUID = 4597863651756274506L;

	private final List<Decoration> decorations;

	private int number;
	private int index;

	private Margin margin;

	private Text text;

	public Page(PageSize pageSize) {
		this.decorations = new ArrayList<Decoration>();
		
		Dimension size = pageSize.getSize();
		setLayout(null);
		setBackground(Color.WHITE);
		setPreferredSize(size);
		setSize(size);
		
		setText(new Text());
		setMargin(new Margin(2, 2, 2, 2));
	}

	@Override
	public int getWidth() {
		return getPreferredSize().width;
	}

	@Override
	public int getHeight() {
		return getPreferredSize().height;
	}
	
	public void addDecoration(Decoration decoration) {
		decorations.add(decoration);
	}

	public void removeDecoration(Decoration decoration) {
		decorations.remove(decoration);
	}
	
	public List<Decoration> getDecorations() {
		return decorations;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public void decore() {
		for (Decoration decoration : decorations) {
			decore(decoration);
		}
	}
	
	public void decore(Decoration decoration) {
		decoration.decore(this);
	}

	public void undecore() {
		for (Decoration decoration : decorations) {
			undecore(decoration);
		}
	}

	public void undecore(Decoration decoration) {
		decoration.undecore(this);
	}

	public Margin getMargin() {
		return margin;
	}

	public void setMargin(Margin margin) {
		this.margin = margin;
		this.text.setBounds(margin.getRectangle(this));
	}

	public Text getText() {
		return text;
	}

	private void setText(Text text) {
		this.text = text;
		add(text);
	}
	
}
